package org.snail.other.controller;

import com.dingtalk.open.client.api.model.corp.Department;
import com.dingtalk.open.client.common.SdkInitException;
import com.dingtalk.open.client.common.ServiceException;
import com.dingtalk.open.client.common.ServiceNotExistException;
import org.apache.log4j.Logger;
import org.snail.GroupTypeDTO;
import org.snail.GroupTypeEnum;
import org.snail.constant.ErrCodeConstant;
import org.snail.common.dingding.exception.OApiException;
import org.snail.employee.bean.EmployeesDO;
import org.snail.exception.BaseException;
import org.snail.exception.CommonException;
import org.snail.exception.EmpException;
import org.snail.exception.JsonResult;
import org.snail.other.bean.GroupCondition;
import org.snail.other.bean.GroupDO;
import org.snail.other.bean.GroupNotificationDTO;
import org.snail.other.bean.GroupNotificationSendDTO;
import org.snail.other.service.impl.GroupServiceImpl;
import org.snail.other.service.impl.SendMessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.*;


/**
 * Created by WJ on 2017/12/12.
 */
@Controller
@RequestMapping("group")
public class GroupController {

    private static final Logger LOGGER =  Logger.getLogger(GroupController.class);

    @Autowired
    private GroupServiceImpl groupServiceImpl ;
    @Autowired
    private SendMessageServiceImpl sendMessageService;

    @RequestMapping("findAllGroup")
    @ResponseBody
    public JsonResult findAllGroup(GroupCondition groupCondition) throws BaseException {
        Object obj = groupCondition.getClass();
        JsonResult result = new JsonResult();
        Map groupDo  = groupServiceImpl.findAllGroupWithPage(groupCondition);
        result.setData(groupDo);
        return result;
    }

    @RequestMapping("saveGroup")
    @ResponseBody
    public JsonResult saveGroup(GroupDO groupDO) throws EmpException {
        JsonResult result = new JsonResult();
        groupServiceImpl.saveGroup(groupDO);
        return result;
    }

    @RequestMapping("deleteGroups")
    @ResponseBody
    public JsonResult deleteGroup(@RequestBody List<Long> groupNameList){
        JsonResult result = new JsonResult();
        groupServiceImpl.deleteGroup(groupNameList);
        return  result;
    }

    @RequestMapping("findEmpByGroupId")
    @ResponseBody
    public JsonResult findEmpByGroupId(GroupCondition groupCondition){
        JsonResult  result  = new JsonResult();
        Map<String, Object> employeesDOList = groupServiceImpl.findEmpByGroupName(groupCondition);
        result.setData(employeesDOList);
        return result;
    }

    /**
     *  查询所有群分类
     * @return
     * @throws JAXBException
     * @throws IOException
     */
    @RequestMapping("findGroupClassification")
    @ResponseBody
    public JsonResult findGroupClassification() throws JAXBException, IOException, BaseException {
        JsonResult  result = new JsonResult();
        Map list = groupServiceImpl.findGroupClassification();
        result.setData(list);
        return result;
    }

//    @RequestMapping("testSaveChartId")
//    @ResponseBody
//    public JsonResult saveChartId() throws FileNotFoundException, BaseException {
//        GroupClassification groupClassification = new GroupClassification();
//        groupClassification.setName("bug小分队");
//        List<Group> groupList = new ArrayList<>();
//        Group group = new Group();
//        group.setGroupName("bug小分队");
//        group.setChatId("chat7eb266bf607e0fbe25541bc9cc23cfd5");
//        group.setGroupClassificationName("bug小分队");
//        groupList.add(group);
//        groupClassification.setGroup(groupList);
//        groupServiceImpl.saveGroupClassification(groupList);
//        JsonResult  result = new JsonResult();
//
//        return result;
//    }

    @RequestMapping("findAllDepartment")
    @ResponseBody
    public JsonResult findAllDepartment() throws ServiceException, ServiceNotExistException, BaseException, SdkInitException, OApiException {
        JsonResult jsonResult = new JsonResult();
        List<Department> departments= groupServiceImpl.findAllDepartment();
        jsonResult.setData(departments);
        return jsonResult;
    }
    /**
     * 删除一个分组
     * @return
     */
    @RequestMapping("deleteClassification")
    @ResponseBody
    public JsonResult deleteClassification(String classificationName) throws BaseException {
        JsonResult result = new JsonResult();
        groupServiceImpl.deleteClassification(classificationName);
        result.setMessage("删除成功");
        return result;
    }
    /**
     * 获取群分类名称
     */
    @RequestMapping("findClassificationNameList")
    @ResponseBody
    public JsonResult findClassificationNameList() throws BaseException {
        JsonResult result = new JsonResult();
        Set<String> classificationNameList = groupServiceImpl.findClassificationNameList();
        result.setMessage("删除成功");
        result.setData(classificationNameList);
        return result;
    }

    /**
     * 获取群分类名称
     */
    @RequestMapping("findClassificationDeptList")
    @ResponseBody
    public JsonResult findClassificationDeptList(String sortName) throws BaseException {
        if (null == sortName || StringUtils.isEmpty(sortName)) {
            LOGGER.error("查询分类下的群组失败！失败原因：群分类名称为空！");
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "查询分类下的群组失败！失败原因：群分类名称为空！");
        }
        JsonResult result = new JsonResult();
        List<GroupDO> classificationDeptList = groupServiceImpl.findClassificationDeptList(sortName);
        result.setMessage("查询成功！");
        result.setData(classificationDeptList);
        return result;
    }

    /**
     * 保存群到指定群分类
     * @return
     * @throws JAXBException
     * @throws IOException
     */
    @RequestMapping("saveGroupClassification")
    @ResponseBody
    public JsonResult saveGroupClassification(@RequestBody GroupNotificationDTO groupNotificationDTO) throws JAXBException, IOException, BaseException {
        JsonResult  result = new JsonResult();
        groupServiceImpl.saveGroupClassification(groupNotificationDTO);
        result.setMessage("保存成功");
        return result;
    }

    @RequestMapping("findGroupById")
    @ResponseBody
    public JsonResult findGroupById(Long groupId){
        JsonResult  result  = new JsonResult();
        GroupDO groupById = groupServiceImpl.findGroupById(groupId);
        result.setData(groupById);
        return result;
    }

    @RequestMapping("findGroupTypeList")
    @ResponseBody
    public JsonResult findGroupTypeList(){
        JsonResult  result  = new JsonResult();
        GroupTypeEnum[] groupTypeEnumArr = GroupTypeEnum.values();
        List<GroupTypeDTO> groupTypeList = new ArrayList();
        for (GroupTypeEnum groupTypeEnum : groupTypeEnumArr) {
            GroupTypeDTO groupTypeDTO = new GroupTypeDTO(groupTypeEnum.getKey(), groupTypeEnum.getValue());
            groupTypeList.add(groupTypeDTO);
        }
        result.setData(groupTypeList);
        result.setCode(JsonResult.SUCCESS);
        return result;
    }

    @RequestMapping("findAPIGroupInfoByGroupId")
    @ResponseBody
    public JsonResult findAPIGroupInfoByGroupId(Long groupId){
        JsonResult  result  = new JsonResult();
        List apiGroupInfoByGroupId = groupServiceImpl.findAPIGroupInfoByGroupId(groupId);
        result.setData(apiGroupInfoByGroupId);
        result.setCode(JsonResult.SUCCESS);
        return result;
    }

    @RequestMapping("findEmpListByDingId")
    @ResponseBody
    public JsonResult findEmpListByDingId(Long groupId){
        JsonResult  result  = new JsonResult();
        List<EmployeesDO> employeesDOS = groupServiceImpl.findEmpListByDingId(groupId);
        result.setData(employeesDOS);
        result.setCode(JsonResult.SUCCESS);
        return result;
    }

    /**
     * 发送群消息通知
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/groupDepartmentNotification", method = RequestMethod.POST)
    public JsonResult  groupDepartmentNotification(@RequestBody GroupNotificationDTO groupNotificationDTO) throws Exception {
        JsonResult result = new JsonResult();
        sendMessageService.groupDepartmentNotification(groupNotificationDTO);
        result.setMessage("消息发送成功");
        return result;
    }

    //钉钉发送文件和图片, 需要返回mediaId mediaType chatId sendType
    @RequestMapping("ddFileUpload")
    @ResponseBody
    public JsonResult testDDFileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request/*,@RequestParam GroupNotificationDTO groupNotificationDTO*/) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<GroupNotificationDTO> groupNotificationDTOList = sendMessageService.DDFileUpload(file,request);
        jsonResult.setData(groupNotificationDTOList);
        jsonResult.setMessage("上传成功");
        return jsonResult;
    }

    //通过钉钉api 发送文件到群
    @RequestMapping("ddFileUpSend")
    @ResponseBody
    public JsonResult ddFileUpSend(@RequestBody GroupNotificationSendDTO groupNotificationSendDTOs) throws BaseException {
        JsonResult jsonResult = new JsonResult();
        sendMessageService.DDFileUpSend(groupNotificationSendDTOs);
        jsonResult.setMessage("发送文件成功");
        return jsonResult;
    }

    //上传连接发送
    @RequestMapping("ddLinkUpload")
    @ResponseBody
    public JsonResult ddLinkSend(@RequestBody GroupNotificationDTO groupNotificationDTO) throws OApiException, BaseException, IOException {
        JsonResult jsonResult = new JsonResult();
        sendMessageService.DDLinkSend(groupNotificationDTO);
        jsonResult.setMessage("链接发送成功");
        return jsonResult;
    }

    @RequestMapping("ddFileDownLoad")
    @ResponseBody
    public void testDDFileDownLoad() throws Exception {
        sendMessageService.DDFileDownLoad();
    }

}
